import { Game_Const } from "./ManifestInfo";


export class VersionTool {
    static unit8ArrayToString(array: Uint8Array): string {
        let out: string;
        let i: number;
        let len: number;
        let c: number;
        let char2: number;
        let char3: number;

        out = "";
        len = array.length;
        i = 0;
        while (i < len) {
            c = array[i++];
            switch (c >> 4) {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                    // 0xxxxxxx
                    out += String.fromCharCode(c);
                    break;
                case 12:
                case 13:
                    // 110x xxxx   10xx xxxx
                    char2 = array[i++];
                    out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f));
                    break;
                case 14:
                    // 1110 xxxx  10xx xxxx  10xx xxxx
                    char2 = array[i++];
                    char3 = array[i++];
                    out += String.fromCharCode(((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((char3 & 0x3f) << 0));
                    break;
            }
        }

        return out;
    }

    /**
     * 加载项目内Manifest文件
     * @returns
     */
    static loadManifest(manifestName: string) {
        return new Promise<cc.Asset>((res, rej) => {
            cc.resources.load(`manifest/${manifestName}`, cc.Asset, (err, resource: cc.Asset) => {
                if (err) {
                    rej(err);
                } else {
                    res(resource);
                }
            });
        });
    }

    /**
     * 获取主包版本
     */
    static async getMianVersion() {
        let manifestData = null;
        if (CC_JSB) {
            let url = jsb.fileUtils.getWritablePath() + "game-remote-asset/project.manifest";
            let buf = jsb.fileUtils.getDataFromFile(url);
            if (buf) {
                manifestData = this.unit8ArrayToString(new Uint8Array(buf));
            }
        }
        let manifest: any;
        if (manifestData) {
            manifest = JSON.parse(manifestData);
        } else {
            try {
                let manifestAsset = await this.loadManifest("prject");
                manifest = JSON.parse(manifestAsset._nativeAsset);
            } catch (error) {
                return `${Game_Const.native_version}.0`;
            }
        }
        let bv = Game_Const.native_version;
        let v = manifest.version ? manifest.version : 0;
        return `${bv}.${v}`;
    }

    /**
     * 子游戏版本
     * @param name 子游戏名
     */
    static async getSubGameVersion(name: string) {
        let manifestData = null;
        console.log('11111111111111111')
        if (CC_JSB) {
            let url = jsb.fileUtils.getWritablePath() + "game-remote-asset/remote/" + name + "/project.manifest";
            let buf = jsb.fileUtils.getDataFromFile(url);
            if (buf) {
                manifestData = this.unit8ArrayToString(new Uint8Array(buf));
            }
        }
        console.log('22222222222222 = '+manifestData)
        let manifest: any;
        if (manifestData) {
            manifest = JSON.parse(manifestData);
        } else {
            try {
                let manifestAsset = await this.loadManifest(name);
                manifest = JSON.parse(manifestAsset._nativeAsset);
            } catch (error) {
                return "0";
            }
        }
        console.log('333333333333333')
        let v = manifest.version ? manifest.version : 0;
        return `${v}`;
    }
}
